<?php
require_once 'libs/Nine/Controller/Action.php';
require_once 'modules/user/models/User.php';
require_once 'modules/content/models/Content.php';
require_once 'modules/condo/models/Condo.php';

class access_IndexController extends Nine_Controller_Action {
	/**
	 * The default action - show the access page
	 */
	public function indexAction() {
		$this->_redirect ( "access/index/login" );
	}
	
	/**
	 * The default action - show the home page
	 */
	public function loginAction() {
		$this->view->headTitle ( 'Login' );
		
		$objUser = new Models_User ();
		
		$loginError = false;
		$submitHandler = Nine_Registry::getAppBaseUrl () . "access/index/login";
		
		$params = $this->_request->getParams ();
		if (isset ( $params ['username'] ) && $params ['username'] != "") {
			
			if ($this->_request->isPost ()) {
				if ($this->auth->login ( $params ['username'], $params ['password'] )) {
					/**
					 * Update last login time
					 */
					
					$objUser->updateLastLogin ( $params ['username'] );
					
					/**
					 * Redirect to your condo
					 */
					
					$user = @reset ( $objUser->getByColumns ( array ('username=?' => $params ['username'] ) )->toArray () );
					if (null == @$user ['condo_id']) {
						$this->_redirect ( "" );
					} else {
						$objCondo = new Models_Condo ();
						$condo = @$objCondo->getCondoById ( @$user ['condo_id'] )->toArray ();
						if (null != @$condo['condo_alias']){
							$this->_redirect ( Nine_Route::_(Nine_Registry::getConfig('liveSite')."{$condo['condo_alias']}") );
						}
					}
					
//					if (null)
//						if ($this->_getCallBackUrl ()) {
//							$this->_redirect ( $this->_getCallBackUrl () );
//						} else {
//							$this->_redirect ( "" );
//						}
				} else {
					$loginError = true;
				}
			} else if ($this->_request->isGet ()) {
				/**
				 * Can reset password, check forgot_password_code and expired time
				 */
				$user = @reset ( $objUser->getByColumns ( array ('username=?' => $params ['username'] ) )->toArray () );
				if (false != @$user && @$user ['forgot_password_code'] == md5 ( $params ['password'] ) && @$user ['forgot_password_expired_date'] > time ()) {
					$this->auth->loginAuto ( $user ['username'] );
					if (2 == $user ['group_id']) {
						$this->_redirect ( 'user/index/edit-employer' );
					} else if (3 == $user ['group_id']) {
						$this->_redirect ( 'user/index/edit-candidate' );
					} else {
						$this->_redirect ( "" );
					}
				} else {
					$loginError = true;
				}
			}
		}
		$this->view->submitHandler = $submitHandler;
		$this->view->loginError = $loginError;
		$this->view->accessMessage = $this->session->accessMessage;
		$this->session->accessMessage = null;
		
		/**
		 * Get content
		 */
		$objContent = new Models_Content ();
		$content = @$objContent->getContentByGid ( 48 )->toArray ();
		
		if (null != @$content['images']) {
			$tmp = explode("||", trim($content['images'],"||"));
			$content['image1'] = @$tmp[0];
			$content['image2'] = Nine_Function::getThumbImage(@$tmp[1],649,108);
		}
		
		/**
		 * Assign to view
		 */
		$this->view->content = $content;
		
		$this->view->forgotPasswordUrl = Nine_Route::_ ( Nine_Registry::getAppBaseUrl () . "user/index/forgot-username-or-password" );
	}
	/**
	 * get and run with submited data
	 */
	public function logoutAction() {
		$this->auth->logout ();
		$this->_redirect ( "" );
	}
}